View Javadoc
1 package net.sourceforge.selfesteem.applet; 2 3 /* 4 * %W% %E% 5 * 6 * Copyright 1997, 1998 by Sun Microsystems, Inc., 7 * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. 8 * All rights reserved. 9 * 10 * This software is the confidential and proprietary information 11 * of Sun Microsystems, Inc. ("Confidential Information"). You 12 * shall not disclose such Confidential Information and shall use 13 * it only in accordance with the terms of the license agreement 14 * you entered into with Sun. 15 */ 16 17 import javax.swing.*; 18 import javax.swing.event.TableModelEvent; 19 import javax.swing.table.TableCellEditor; 20 import javax.swing.table.TableCellRenderer; 21 import javax.swing.tree.DefaultTreeSelectionModel; 22 import javax.swing.tree.TreeModel; 23 import java.awt.*; 24 25 /*** 26 * This example shows how to create a simple JTreeTable component, 27 * by using a JTree as a renderer (and editor) for the cells in a 28 * particular column in the JTable. 29 * 30 * @version %I% %G% 31 * 32 * @author Philip Milne 33 * @author Scott Violet 34 */ 35 36 public class JTreeTable extends JTable { 37 public TreeTableCellRenderer tree; 38 39 public JTreeTable(TreeModel treeModel) { 40 super(); 41 42 // Create the tree. It will be used as a renderer and editor. 43 tree = new TreeTableCellRenderer(treeModel); 44 45 // Install a tableModel representing the visible rows in the tree. 46 super.setModel(new TreeTableModelAdapter(tree)); 47 48 // Force the JTable and JTree to share their row selection models. 49 tree.setSelectionModel(new DefaultTreeSelectionModel() { 50 // Extend the implementation of the constructor, as if: 51 /* public this() */ 52 { 53 setSelectionModel(listSelectionModel); 54 } 55 }); 56 // Make the tree and table row heights the same. 57 setRowHeight(24); 58 tree.setRowHeight(24); 59 60 // Install the tree editor renderer and editor. 61 setDefaultRenderer(TreeTableModel.class, tree); 62 setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor()); 63 64 setShowGrid(false); 65 setIntercellSpacing(new Dimension(0, 0)); 66 setTableHeader(null); 67 setBackground(Color.white); 68 } 69 70 public void tableChanged(TableModelEvent e) { 71 super.tableChanged(e); 72 } 73 74 /* Workaround for BasicTableUI anomaly. Make sure the UI never tries to 75 * paint the editor. The UI currently uses different techniques to 76 * paint the renderers and editors and overriding setBounds() below 77 * is not the right thing to do for an editor. Returning -1 for the 78 * editing row in this case, ensures the editor is never painted. 79 */ 80 public int getEditingRow() { 81 return (getColumnClass(editingColumn) == TreeTableModel.class) ? -1 : editingRow; 82 } 83 84 // 85 // The renderer used to display the tree nodes, a JTree. 86 // 87 88 public class TreeTableCellRenderer extends JTree implements TableCellRenderer { 89 protected int visibleRow; 90 91 public TreeTableCellRenderer(TreeModel model) { 92 super(model); 93 } 94 95 public void setBounds(int x, int y, int w, int h) { 96 super.setBounds(x, 0, w, JTreeTable.this.getHeight()); 97 } 98 99 public void paint(Graphics g) { 100 g.translate(0, -visibleRow * getRowHeight()); 101 super.paint(g); 102 } 103 104 public Component getTableCellRendererComponent(JTable table, 105 Object value, 106 boolean isSelected, 107 boolean hasFocus, 108 int row, int column) { 109 visibleRow = row; 110 return this; 111 } 112 } 113 114 // 115 // The editor used to interact with tree nodes, a JTree. 116 // 117 118 public class TreeTableCellEditor extends AbstractCellEditor implements TableCellEditor { 119 public Component getTableCellEditorComponent(JTable table, Object value, 120 boolean isSelected, int r, int c) { 121 return tree; 122 } 123 } 124 125 }

This page was automatically generated by Maven